<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="articleTitle">
        <el-input
          v-model="queryParams.articleTitle"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示" prop="isDisplay">
        <el-select v-model="queryParams.isDisplay" placeholder="请选择显示状态" clearable>
          <el-option label="显示" value="1" />
          <el-option label="隐藏" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增子公司</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="subsidiaryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="公司LOGO" align="center" width="100">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.articleLogo"
            :src="scope.row.articleLogo"
            :preview-src-list="[scope.row.articleLogo]"
            style="width: 50px; height: 50px; border-radius: 8px;"
            fit="cover"
          />
          <span v-else>暂无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="articleTitle" />
      <el-table-column label="公司简介" align="center" prop="articleDescribe" show-overflow-tooltip />
      <el-table-column label="是否显示" align="center" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isDisplay === 1 ? 'success' : 'danger'">
            {{ scope.row.isDisplay === 1 ? '显示' : '隐藏' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" width="80" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改子公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="articleTitle">
              <el-input v-model="form.articleTitle" placeholder="请输入公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" :min="0" :max="999" placeholder="排序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="公司简介" prop="articleDescribe">
          <el-input v-model="form.articleDescribe" type="textarea" :rows="3" placeholder="请输入公司简介" />
        </el-form-item>
        <el-form-item label="公司LOGO" prop="articleLogo">
          <image-upload
            v-model="form.articleLogo"
            @input="uploadSuccess"
            action="/minio/upload"
            :limit="1"
            :fileSize="2"
          />
        </el-form-item>
        <el-form-item label="是否显示" prop="isDisplay">
          <el-radio-group v-model="form.isDisplay">
            <el-radio :label="1">显示</el-radio>
            <el-radio :label="0">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="详细介绍" prop="articleContent">
          <MyAiEditor v-model="form.articleContent" :min-height="200" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArticle, saveArticle, deleteArticle } from "@/api/system/frontAdmin"
import { listData } from "@/api/system/dict/data"

export default {
  name: "SubsidiaryManagement",
  data() {
    return {
      module: '组织架构',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 子公司表格数据
      subsidiaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleTitle: null,
        isDisplay: null,
        frontMenuId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        articleTitle: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        articleDescribe: [
          { required: true, message: "公司简介不能为空", trigger: "blur" }
        ],
        articleLogo: [
          { required: true, message: "公司LOGO不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ]
      },
      menuDict: {}
    }
  },
  created() {
    this.initMenuDict()
  },
  methods: {
    /** 初始化菜单字典 */
    initMenuDict() {
      listData({'dictType': 'article_menu_association', 'dictLabel': this.module}).then(response => {
        const rows = response.rows
        this.menuDict = rows ? rows[0] : {}
        this.queryParams.frontMenuId = this.menuDict.dictValue
        this.getList()
      })
    },
    /** 查询子公司列表 */
    getList() {
      this.loading = true
      listArticle(this.queryParams).then(response => {
        this.subsidiaryList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 上传成功回调
    uploadSuccess(images) {
      this.form.articleLogo = images
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        frontMenuId: this.menuDict.dictValue,
        frontMenuName: this.module,
        articleTitle: null,
        articleDescribe: null,
        articleLogo: null,
        articleContent: null,
        isDisplay: 1,
        sort: 1
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加子公司"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids[0]
      const subsidiary = this.subsidiaryList.find(item => item.id === id)
      if (subsidiary) {
        this.form = { ...subsidiary }
        this.open = true
        this.title = "修改子公司"
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.frontMenuId = this.menuDict.dictValue
          this.form.frontMenuName = this.module
          saveArticle(this.form).then(response => {
            this.$modal.msgSuccess(this.form.id ? "修改成功" : "新增成功")
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      const names = row.articleTitle || this.subsidiaryList.filter(item => this.ids.includes(item.id)).map(item => item.articleTitle).join(', ')
      this.$modal.confirm('是否确认删除子公司"' + names + '"？').then(function() {
        return deleteArticle(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>